﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BahdlClassLib;
using BusinessLayer;

namespace MasterForms
{
    public partial class LeaveTypes : CustomFormTemplate
    {
        public LeaveTypes()
        {
            InitializeComponent();
        }
       
        void LeaveTypes_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == System.Windows.Forms.Keys.Escape)
            {
                if (this.Tag.ToString() == "view")
                {
                    LeaveTypes_ToolBarButtonClick("exit");
                }
                else
                {
                    LeaveTypes_ToolBarButtonClick("revert");
                }
            }
        }
        void LeaveTypes_EnableControls(bool Enable)
        {
            fraDtl.Enabled = Enable;
        }
        private void LeaveTypes_ToolBarButtonClick(string Button)
        {
            if (CommonFunction.InList(Button, "add", "edit", "delete") == true)
            {
                if ((Button == "edit" || Button == "delete"))
                {
                    if (txtDesignation.Text == "")
                    {
                        return;
                    }
                }
            }
            switch (Button)
            {
                case "exit":
                    if (UIFunction.SureToExit("") == true)
                    {
                        this.Close();
                    }
                    break;
                case "revert":
                    this.Tag = "view";
                    LeaveTypes_EnableControls(false);
                    this.ToolBarEnterEditMode = false;
                    break;
                case "print":
                    //CrytalEditor newObject = new CrytalEditor();
                    //DataSet dsLocal = new DataSet();
                    //string CompanyDescr = getCompanyData();
                    //CompanyDescr = CompanyDescr + "Raw Material Checklist";
                    //try {
                    //    OpenBlClass();
                    //    dsLocal.Tables.Add(blRM.GetDataTable("Select * From Mst_Authority where Cocd = " + objCompany.Code + " Order by Descr", "mst_Approval"));
                    //    newObject.LoadPreviewer("Rep_Mst_Approval.rpt", dsLocal, enmReportType.FAMasters, CompanyDescr);
                    //} catch {
                    //} finally {
                    //    CloseBlClass();
                    //    newObject = null;
                    //    dsLocal = null;
                    //}
                    break;
                case "find":
                    DataSet dsLocal = new DataSet();
                    Popup popup = new Popup();
                    try
                    {
                        ClsLeaveTypes ClsLeaveTypes = new ClsLeaveTypes();
                        dsLocal.Tables.Add(ClsLeaveTypes.GetAllLeaveTypes());
                        dsLocal.Tables[0].TableName = "popup";
                        popup.Width = 50;
                        popup.ShowPopup(null, "LeaveTypeID", ref dsLocal, "popup", "LeaveTypeID,LeaveType", "", "Leave Type Master", "ID,LeaveType", "10,20");
                        if (!string.IsNullOrEmpty(CommonFunction.NullToString(popup.ReturnValue())))
                        {
                            uiGetData(Convert.ToInt32(popup.Field("LeaveTypeID")));
                        }
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    finally
                    {
                        dsLocal = null;
                        popup = null;
                    }
                    break;
                case "add":
                    if (uiGetData(0))
                    {
                        this.ToolBarEnterEditMode = true;
                        this.Tag = "add";
                        LeaveTypes_EnableControls(true);
                        txtDesignation.Select();
                    }
                    break;
                case "edit":
                    this.ToolBarEnterEditMode = true;
                    this.Tag = "edit";
                    LeaveTypes_EnableControls(true);
                    txtDesignation.Select();
                    break;
                case "save":
                    if (uiPutData() == true & this.Tag.ToString() == "edit")
                    {
                        this.Tag = "view";
                        this.ToolBarEnterEditMode = false;
                        LeaveTypes_EnableControls(false);
                    }
                    break;
                case "delete":
                    uiDeleteData();
                    break;
            }


        }


        private void uiDeleteData()
        {
            try
            {
                ClsLeaveTypes ClsLeaveTypes = new ClsLeaveTypes();
                ClsLeaveTypes.LeaveTypeID = Convert.ToInt32(txtDesignation.Tag);
                ClsLeaveTypes.DeleteLeaveType();
                uiGetData(0);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
        }
        private bool uiPutData()
        {
            bool functionReturnValue = false;
            try
            {
                ClsLeaveTypes ClsLeaveTypes = new ClsLeaveTypes();
                ClsLeaveTypes.LeaveType = txtDesignation.Text.Trim();
                ClsLeaveTypes.LeaveTypeID = Convert.ToInt32(txtDesignation.Tag);
                if (ClsLeaveTypes.SaveLeaveType(this.Tag.ToString()))
                    functionReturnValue = true;
                uiGetData(0);
                if (this.Tag.ToString() == "add")
                    txtDesignation.Select();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }
        private bool uiGetData(int KeyField)
        {
            bool functionReturnValue = false;
            try
            {
                ClsLeaveTypes ClsLeaveType = new ClsLeaveTypes();
                ClsLeaveType.GetLeaveType(KeyField);
                //Need to check here/////////////////////////////////////////////////////////////////////////////////////
               // txtDesignation.Tag = ClsLeaveType.LeaveTypeID;
                txtDesignation.Tag = KeyField;
                txtDesignation.Text = ClsLeaveType.LeaveType;
                functionReturnValue = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {

            }
            return functionReturnValue;
        }

        private void LeaveTypes_Load(object sender, EventArgs e)
        {
            this.Left = 0;
            this.Top = 0;
            this.Tag = "view";
            txtDesignation.Text = "";

            base.ToolBarButtonClick += new ToolBarButtonClickEventHandler(LeaveTypes_ToolBarButtonClick);
            base.EnableControls += new EnableControlsEventHandler(LeaveTypes_EnableControls);
            base.KeyDown += new KeyEventHandler(LeaveTypes_KeyDown);
        }
       
    }
}
